package com.gitee.melin.bee.core.jdbc.dialect.phoenix;

import com.gitee.melin.bee.core.jdbc.dialect.AbstractDBQuery;

public class PhoenixQuery extends AbstractDBQuery {

    /** 不指定schema列信息模板SQL */
    public static final String QUERY_COLUMNS_SQL_DEFAULT =
            " select COLUMN_NAME,COLUMN_FAMILY,DATA_TYPE,KEY_SEQ,NULLABLE, '' as CHARACTER_SET_NAME,"
                    + " '' as COLLATION_NAME ,'' as ORDINAL_POSITION , 0 as NUMERIC_PRECISION, 0 as NUMERIC_SCALE, '' as AUTO_INCREMENT from SYSTEM.CATALOG where TABLE_NAME='%s' and COLUMN_NAME is not null ";
    /** 查询默认指定列信息模板SQL */
    public static final String QUERY_COLUMNS_SQL = QUERY_COLUMNS_SQL_DEFAULT + "  AND TABLE_SCHEM = '%s' ";

    /** 不指定schema查询table信息模板SQL */
    public static final String QUERY_TABLE_BY_SCHEMA_SQL_DEFAULT =
            " select TABLE_NAME,TABLE_SCHEM,TABLE_TYPE,SCOPE_CATALOG as CATALOG,'' as ENGINE,'' as OPTIONS, 0 as ROWSNUM, null as CREATE_TIME, null as UPDATE_TIME "
                    + "from SYSTEM.CATALOG where TABLE_TYPE in ('u','v')  ";
    /** 根据schema查询table信息模板SQL */
    public static final String QUERY_TABLE_BY_SCHEMA_SQL =
            QUERY_TABLE_BY_SCHEMA_SQL_DEFAULT + "  AND TABLE_SCHEM = '%s' ";

    @Override
    public String tablesSql(String schemaName) {
        if (schemaName == null || schemaName.isEmpty()) {
            return QUERY_TABLE_BY_SCHEMA_SQL_DEFAULT;
        }
        return String.format(QUERY_TABLE_BY_SCHEMA_SQL, schemaName);
    }

    @Override
    public String columnsSql(String schemaName, String tableName) {
        if (schemaName == null || schemaName.isEmpty()) {
            return String.format(QUERY_COLUMNS_SQL_DEFAULT, tableName);
        }
        return String.format(QUERY_COLUMNS_SQL, tableName, schemaName);
    }

    @Override
    public String schemaName() {
        return "TABLE_SCHEM";
    }

    @Override
    public String tableName() {
        return "TABLE_NAME";
    }

    @Override
    public String tableType() {
        return "TABLE_TYPE";
    }

    @Override
    public String tableComment() {
        return "TABLE_NAME";
    }

    @Override
    public String columnName() {
        return "COLUMN_NAME";
    }

    @Override
    public String columnType() {
        return "DATA_TYPE";
    }

    @Override
    public String columnComment() {
        return "COLUMN_NAME";
    }

    @Override
    public String columnKey() {
        return "KEY_SEQ";
    }

    public String isNullable() {
        return "NULLABLE";
    }

    @Override
    public String rows() {
        return "ROWSNUM";
    }
}
